unit FormCadastroPlantasUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.Grids,
  Vcl.DBGrids, JvExDBGrids, JvDBGrid, System.Actions, Vcl.ActnList;

type
  TFormCadastroPlantas = class(TForm)
    EditNome: TEdit;
    Label1: TLabel;
    JvDBGrid1: TJvDBGrid;
    BitBtnSair: TBitBtn;
    BitBtnSalvar: TBitBtn;
    BitBtnEditar: TBitBtn;
    ActionList1: TActionList;
    ActionESC: TAction;
    procedure FormCreate(Sender: TObject);
    procedure BitBtnSalvarClick(Sender: TObject);
    Procedure BuscaID(id : string);
    procedure BitBtnSairClick(Sender: TObject);
    procedure BitBtnEditarClick(Sender: TObject);
    procedure ActionESCExecute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    modeEdicao : Boolean;
  end;

var
  FormCadastroPlantas: TFormCadastroPlantas;

implementation

{$R *.dfm}

uses DMDadosUnt;

procedure TFormCadastroPlantas.ActionESCExecute(Sender: TObject);
begin
  close;
end;

procedure TFormCadastroPlantas.BitBtnEditarClick(Sender: TObject);
begin
 DMDados.cdsCadastroPlanta.Edit;
 EditNome.Text    := DMDados.cdsCadastroPlantaNOME.Value;
 modeEdicao       := True;
end;

procedure TFormCadastroPlantas.BitBtnSairClick(Sender: TObject);
begin
close;
end;

procedure TFormCadastroPlantas.BitBtnSalvarClick(Sender: TObject);
begin
  if EditNome.Text = '' then
  begin
   MessageDlg('Informe um nome da Planta',mtInformation,[mbOK],0);
   exit;

  end;



  if modeEdicao then
  begin



    DMDados.cdsCadastroPlanta.Edit;
    DMDados.cdsCadastroPlantaNOME.AsString       := EditNome.Text;

    DMDados.cdsCadastroPlanta.ApplyUpdates(-1);
    DMDados.cdsCadastroPlanta.Refresh;

    EditNome.Text    := '';

    modeEdicao := False;

    exit;

  end;


  BuscaID('ID_PLANTA');

  DMDados.cdsCadastroPlanta.Open;
  DMDados.cdsCadastroPlanta.Append;

  DMDados.cdsCadastroPlantaID_PLANTA.AsInteger   := DMDados.cdsControleVALOR.AsInteger+1;
  DMDados.cdsCadastroPlantaNOME.AsString         := EditNome.Text;

  DMDados.cdsCadastroPlanta.ApplyUpdates(-1);
  DMDados.cdsCadastroPlanta.Refresh;

  DMDados.cdsControle.Edit;
  DMDados.cdsControleVALOR.AsInteger := DMDados.cdsControleVALOR.AsInteger + 1;
  DMDados.cdsControle.ApplyUpdates(-1);

  EditNome.Text := '';

end;

procedure TFormCadastroPlantas.BuscaID(id: string);
begin
  DMDados.cdsControle.Close;
  DMDados.cdsControle.Params[0].AsString := id;
  DMDados.cdsControle.Open;
end;

procedure TFormCadastroPlantas.FormCreate(Sender: TObject);
begin
  DMDados.cdsCadastroPlanta.Open;
end;

end.
